package org.zpmis.api.business;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.zpmis.common.base.BaseWebService;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.domain.customer.qo.StatisticsQo;
import org.zpmis.domain.customer.service.CustomerPlanService;
import org.zpmis.domain.customer.service.IntentionService;
import org.zpmis.domain.customer.service.ReturnVisitService;
import org.zpmis.domain.customer.vo.IntentionEfficientVo;
import org.zpmis.domain.customer.vo.StatisticsVo;
import org.zpmis.domain.order.service.OrderService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with CodeGenerator
 * Description:统计分析业务处理
 * @author  Yang
 * Date: 2021-6-2
 * Time: 14:53:29
 */
@Service
@Slf4j
public class StatisticsWebService extends BaseWebService {

     @Reference
     private IntentionService intentionService;

     @Reference
     private CustomerPlanService customerPlanService;

     @Reference
     private ReturnVisitService returnVisitService;

     @Reference
     private OrderService orderService;
    /**
     * 漏斗图统计
     * @param statisticsQo
     */
    public List<StatisticsVo> funnelStatistics(StatisticsQo statisticsQo) throws BusinessException,Exception {
        List<StatisticsVo> resultList = new ArrayList<>();
        Long startTime = statisticsQo.getStartTime();
        Long endTime = statisticsQo.getEndTime();
        //获取意向数量
        Long intentionCount = intentionService.getIntentionCount(startTime,endTime);
        resultList.add(StatisticsVo.builder().name("意向").value(intentionCount).build());
        //获取回访数量
        Long returnVisitCount = returnVisitService.getReturnVisitCount(startTime, endTime);
        resultList.add(StatisticsVo.builder().name("回访").value(returnVisitCount).build());
        //获取计划数量
        Long customerPlanCount = customerPlanService.getCustomerPlanCount(startTime, endTime);
        resultList.add(StatisticsVo.builder().name("计划").value(customerPlanCount).build());
        //获取回访成功数量
        Long successReturnVisitCount = returnVisitService.getSuccessReturnVisitCount(startTime, endTime);
        resultList.add(StatisticsVo.builder().name("回访成功").value(successReturnVisitCount).build());
        //获取订单数量
        Long orderCount = orderService.getOrderCount(startTime, endTime);
        resultList.add(StatisticsVo.builder().name("订单").value(orderCount).build());

        return resultList;
    }


    /**
     * 获取意向有效率
     * @param statisticsQo
     * @return
     */
    public List<List<String>> intentEfficient(StatisticsQo statisticsQo) {
        List<List<String>> resultList = new ArrayList<>();
        resultList.add(Arrays.asList("product","有效用户数","总意向数"));

        List<IntentionEfficientVo> intentionEfficientVoList = intentionService.getIntentionEfficient(statisticsQo.getStartTime(), statisticsQo.getEndTime());
        if(intentionEfficientVoList != null && !intentionEfficientVoList.isEmpty()){
            intentionEfficientVoList.stream().forEach(intentionEfficientVo -> {
                resultList.add(Arrays.asList(intentionEfficientVo.getCDate(),intentionEfficientVo.getVCount(),intentionEfficientVo.getTotalCount()));
            });
        }
        return resultList;
    }
}
